package com.icast.ui;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

import com.icast.api.ICast;
import com.smit.dvb.CIMenuInfo;

public class DialogCi extends Dialog {
	private static Context mContext;

	public DialogCi(Context context) {
		super(context);
		mContext = context;
	}

	TextView subTitleTextView;
	TextView titleTitleTextView;
	TextView bottomTextView;

	public void initView() {
		LinearLayout linearLayoutMain = new LinearLayout(mContext);
		linearLayoutMain.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		linearLayoutMain.setOrientation(LinearLayout.VERTICAL);
		linearLayoutMain.setBackgroundColor(0xff333333);
		linearLayoutMain.setPadding(50, 30, 50, 30);

		CIMenuInfo ciMenuInfo = ICast.getCIMenuInfo();

		String subTitleText = ciMenuInfo.subTitleText;
		subTitleTextView = new TextView(mContext);
		subTitleTextView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		subTitleTextView.setText(subTitleText);
		subTitleTextView.setGravity(Gravity.CENTER);
		subTitleTextView.setTextSize(30);
		linearLayoutMain.addView(subTitleTextView);

		String titleText = ciMenuInfo.titleText;
		titleTitleTextView = new TextView(mContext);
		titleTitleTextView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		titleTitleTextView.setText(titleText);
		titleTitleTextView.setGravity(Gravity.CENTER);
		titleTitleTextView.setTextSize(20);
		linearLayoutMain.addView(titleTitleTextView);

		itemTextList = ciMenuInfo.itemTextList;
		ListView listView = new ListView(mContext);
		listView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		listView.setPadding(0, 20, 0, 20);
		myAdapter = new MyAdapter(mContext);
		listView.setAdapter(myAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				ICast.selectCIMenuItem(arg2 + 1);
			}
		});
		linearLayoutMain.addView(listView);

		String bottomText = ciMenuInfo.bottomText;
		bottomTextView = new TextView(mContext);
		bottomTextView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		bottomTextView.setText(bottomText);
		bottomTextView.setGravity(Gravity.CENTER);
		bottomTextView.setTextSize(20);
		linearLayoutMain.addView(bottomTextView);

		setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK && KeyEvent.ACTION_DOWN != event.getAction()) {
					ICast.selectCIMenuItem(0);
					return true;
				} else {
					return false;
				}
			}
		});

		getWindow().setContentView(linearLayoutMain);
	}

	public void refresh() {
		CIMenuInfo ciMenuInfo = ICast.getCIMenuInfo();
		String subTitleText = ciMenuInfo.subTitleText;
		subTitleTextView.setText(subTitleText);
		String titleText = ciMenuInfo.titleText;
		titleTitleTextView.setText(titleText);
		String bottomText = ciMenuInfo.bottomText;
		bottomTextView.setText(bottomText);
		itemTextList = ciMenuInfo.itemTextList;
		myAdapter.notifyDataSetChanged();
	}

	private MyAdapter myAdapter;
	private ArrayList<String> itemTextList;

	public class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return itemTextList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item_list, null);
			}
			TextView text = (TextView) convertView.findViewById(R.id.button);
			text.setText(itemTextList.get(position));
			return convertView;
		}
	}
}